import QtQuick 1.1
//import QtMobility.gallery 1.1
import com.nokia.meego 1.0
import Qt.labs.folderlistmodel 1.0

/* idea from ereader. */

Page {
    id: pageFileListView

    tools: ToolBarLayout {
        opacity: userTheme.opacityToolbar
        ToolIcon {
            //iconId: "toolbar-back"
            iconSource: "images/toolbar/back.png"
            onClicked: {
                pageStack.pop()
            }
        }
        ToolIcon {
            //iconId: "toolbar-up"
            iconSource: "images/toolbar/folder_back.png"
            onClicked: {
                if (stripFilePrefix(folderModel.folder) !== "/") {
                    setCurrentFolder(stripFilePrefix(stripSuffix(folderModel.folder)))
                }
            }
        }
    }

    BackgroundPage { }

    function stripFilePrefix(path) {
        var filePath = new String(path)
        return filePath.replace("file://", "")
    }

    function stripSuffix(path) {
        var filePath = new String(path)
        return filePath.substring(0, filePath.lastIndexOf("/"))
    }

    function setCurrentFolder(folder) {
        folderModel.folder = folder
    }

    QNMTitle {
        id: titlePage
        text: stripFilePrefix(folderModel.folder)
        image: "images/folder_docs.png"
        sizFont: 22
        busy: false
    }

    Component {
        id: delegateFolder
        Rectangle {
            id:wrapper
            height: 70
            width: list.width
            color: "transparent"
            radius: 10
            Behavior on opacity { NumberAnimation{ duration: 400 } }
            Image {
                id: imgFolder
                anchors.verticalCenter: parent.verticalCenter
                anchors.left: parent.left
                anchors.leftMargin: 20
                source: folderModel.isFolder(index) ? "images/folder.png" : "images/doc.png"
            }
            Text {
                text: decodeURI(fileName)
                anchors.verticalCenter: parent.verticalCenter
                anchors.left: imgFolder.right
                anchors.leftMargin: 20
                font.pixelSize: 30
                color: folderModel.isFolder(index) ? "black" : "blue"
            }

            MouseArea {
                id: maFolder
                anchors.fill: parent
                onClicked: {
                    if (folderModel.isFolder(index)) {
                        setCurrentFolder(filePath)
                    }
                    else {
                        setCurrentFolder(folderModel.folder)
                        //console.log("Selected file: "+stripFilePrefix(decodeURI(filePath)) )
                        pageImportWords.xmlfile = stripFilePrefix(decodeURI(filePath))
                        //pageImportWords._txtTF.text = stripFilePrefix(decodeURI(filePath))
                        pageStack.pop()
                    }
                }

                onExited: {
                    wrapper.color = "transparent"
                    wrapper.opacity = 1
                }

                onPressedChanged: {
                    if( pressed ) {
                        wrapper.color = "black"
                        wrapper.opacity = 0.5
                    } else {
                        wrapper.color = "transparent"
                        wrapper.opacity = 1
                    }
                }

            } //MouseArea

        }
    }


    ListView {
        id: list
        clip: true
        anchors.top: titlePage.bottom
        anchors.bottom: parent.bottom
        width: parent.width
        model: folderModel
        spacing: 2
        section.property: "path"
        delegate:  delegateFolder
    }

    SectionScroller {
        listView: list
    }

    ScrollDecorator { flickableItem: list }

    FolderListModel {
        id: folderModel
        nameFilters: [ "*.xml", "*.XML" ]
        sortField: "Type"
    }

    onStatusChanged:  {
        if (status == PageStatus.Active) {
            /*
            if (engine.pathExist(lastPath)) {
                setCurrentFolder(lastPath)
            }
            else {
                setCurrentFolder("/home/user/MyDocs")
            }*/
        }/* else if (status == PageStatus.Activating) {
        }*/
    }

}


